<template>
  <div>
    <div class="warn-top">
      <h4 style="text-align: left; color: #126bff; margin-bottom: 20px">
        访客登记管理
      </h4>
      <!--         :rules="rules" -->
      <el-form
        ref="elForm"
        :model="formData"
        size="medium"
        label-width="120px"
        style="display: flex"
        id="elform"
      >
        <el-form-item
          label="学校名称："
          prop="school_name"
          style="width: 450px; float: left"
        >
          <el-input
            v-model="formData.school_name"
            placeholder="请输入学校名称"
            clearable
            style="float: left"
            :style="{ width: '300px' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item
          label="被访者姓名："
          prop="respondent"
          style="width: 450px"
        >
          <el-input
            v-model="formData.respondent"
            placeholder="请输入被访者姓名"
            clearable
            style="float: left"
            :style="{ width: '300px' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item
          label="访客姓名："
          prop="visitor_name"
          style="width: 450px"
        >
          <el-input
            v-model="formData.visitor_name"
            placeholder="请输入访客姓名"
            clearable
            style="float: left"
            :style="{ width: '300px' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item
          label="车牌号码："
          prop="plate_number"
          style="width: 450px"
        >
          <el-input
            v-model="formData.plate_number"
            placeholder="请输入车牌号码"
            clearable
            style="float: left"
            :style="{ width: '300px' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="登记时段：" prop="time" style="width: 450px">
          <el-date-picker
            type="daterange"
            v-model="formData.time"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{ width: '60%' }"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            range-separator="至"
            clearable
          ></el-date-picker>
        </el-form-item>
        <el-form-item style="float: float">
          <el-button
            @click="resetForm()"
            size="medium"
            style="color: #126bff; background-color: #e6effa"
            >重置</el-button
          >
          <el-button
            type="primary"
            size="medium"
            @click="submitForm(formData)"
            style="color: #ffffff; background-color: #126bff"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="warn-content" style="width: 100%">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="序号" width="100">
          <template slot-scope="scope">
            {{ scope.$index + 1 + (currentPage - 1) * pageSize }}
          </template>
        </el-table-column>
        <el-table-column prop="school_name" label="学校名称"> </el-table-column>
        <el-table-column prop="respondent" label="被访者姓名" width="100">
        </el-table-column>
        <el-table-column prop="visitor_name" label="访客姓名">
        </el-table-column>
        <el-table-column prop="visitor_phone" label="访客手机号" v-if="false">
        </el-table-column>
        <el-table-column
          prop="tm_visitor_phone"
          label="脱敏访客手机号"
          v-if="false"
        >
        </el-table-column>
        <el-table-column prop="visitor_identity" label="证件号码" v-if="false">
        </el-table-column>
        <el-table-column
          prop="tm_visitor_identity"
          label="脱敏证件号码"
          v-if="false"
        >
        </el-table-column>
        <el-table-column prop="visitor_sex" label="访客性别">
          <template v-slot="scope">
            <span v-if="scope.row.visitor_sex == 1">男</span>
            <span v-else>女</span>
          </template>
        </el-table-column>
        <el-table-column prop="is_drive" label="是否驾车">
          <template v-slot="scope">
            <span v-if="scope.row.is_drive == 1">是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>
        <el-table-column prop="plate_number" label="车牌号码">
          <template v-slot="scope">
            <span v-if="scope.row.plate_number == ''">-</span>
            <span v-else>{{ scope.row.plate_number }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="relation" label="与被访者关系">
        </el-table-column>
        <el-table-column prop="reason" label="访客事由"> </el-table-column>
        <el-table-column prop="address" label="到访地点"> </el-table-column>
        <el-table-column prop="department_name" label="所属派出所" v-if="false">
        </el-table-column>
        <el-table-column prop="visitor_sex" label="性别" v-if="false">
        </el-table-column>
        <el-table-column prop="visitor_identity" label="证件号码" v-if="false">
        </el-table-column>
        <el-table-column prop="pic" label="访客人像" v-if="false">
          <template v-slot="scope">
            <img
              style="width: 40px; height: 40px"
              :src="scope.row.pic"
              alt=""
            />
            <!-- </div> -->
          </template>
        </el-table-column>

        <!-- department_name: "爱民西道派出所",
          sex: "女",
          zjhm: "413046465456465465",
          pic: require("@/assets/pagesImg/4392.png"), -->
        <el-table-column prop="status" label="核验结果">
          <template v-slot="scope">
            <!-- {{ scope.row.status }} -->
            <!-- <el-tag size="medium">{{ scope.row.name }}</el-tag> -->
            <el-tag
              size="medium"
              v-if="scope.row.status == 4"
              style="
                width: 70px;
                height: 30px;
                background: #ffffff;
                border-radius: 4px 4px 4px 4px;
                opacity: 1;
                color: #1cdc7c;
                border: 1px solid #1cdc7c;
                line-height: 30px;
              "
              >人员正常</el-tag
            >
            <el-tag
              size="medium"
              v-if="scope.row.status == 3"
              style="
                width: 70px;
                height: 30px;
                background: #ffffff;
                border-radius: 4px 4px 4px 4px;
                opacity: 1;
                border: 1px solid #ff4444;
                color: #ff4444;
                line-height: 30px;
              "
              >待处理</el-tag
            >
            <el-tag
              size="medium"
              v-if="scope.row.status == 5"
              style="
                width: 70px;
                height: 30px;
                background: #ffffff;
                border-radius: 4px 4px 4px 4px;
                opacity: 1;
                border: 1px solid #ff4444;
                color: #ff4444;
                line-height: 30px;
              "
              >人员异常</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="登记时间" width="200">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >详情</el-button
            >
            <!-- <el-button type="text" size="small">编辑</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex; flex-direction: row-reverse">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[20, 50, 100]"
          :page-size="pageSize"
          layout="total, prev, pager, next, sizes"
          :total="total"
          style="float: right"
        >
        </el-pagination>
      </div>
    </div>
    <div>
      <detailDialog
        :dialogVsible="Visible"
        :detail="detaile"
        :visitorVisible="visitorVisible"
        @handel="handel"
        @Confirm="Confirm"
      ></detailDialog>
    </div>
  </div>
</template>
<script>
import detailDialog from "@/components/detailDetail";
import { visitorLog } from "@/api/volunteer";
import { Loading } from "element-ui";
export default {
  name: "WARN",
  components: { detailDialog },
  data() {
    return {
      total: "",
      Visible: false,
      visitorVisible: false,
      userInfo: JSON.parse(localStorage.getItem("userInfo")),
      currentPage4: 4,
      // pages: {
      // 按页数查询数据
      currentPage: 1,
      pageSize: 20,
      // },
      formData: {
        school_name: "", // 学校名称
        respondent: "", // 被访者姓名
        visitor_name: "", //访客姓名
        plate_number: "",
        time: "",
      },
      searchData: {
        school_name: "", // 学校名称
        respondent: "", // 被访者姓名
        visitor_name: "", //访客姓名
        plate_number: "",
        page: "1",
        pagesize: "20",
      },
      detaile: {},
      tableData: [],
      datalist: [],
      dialogVisible: false,
      // searchData: {
      //   page: 1,
      //   pagesize: 20,
      // },
    };
  },
  mounted() {
    this.initVisitorLog();
  },

  methods: {
    initVisitorLog() {
      let that = this;
      let loadingInstance = Loading.service({
        target: document.querySelector(".el-table__body-wrapper"),
      });
      visitorLog(this.searchData).then((res) => {
        console.log(res);
        if (res.code == 200) {
          console.log(res.data.res);
          res.data.res.forEach((item, index) => {
            res.data.res[index].create_time = that
              .$moment(item.create_time * 1000)
              .format("YYYY-MM-DD HH:mm:ss");
            // res.data.res[index].update_time = that
            //   .$moment(item.update_time * 1000)
            //   .format("YYYY-MM-DD HH:mm:ss");

            // console.log(moment(item.create_time * 1000).format("YYYY-MM-DD"));
          });
          that.total = res.data.count;
          this.tableData = res.data.res;
          loadingInstance.close();
          // console.log(that.tableData.length);
          // console.log(res.data.count);
          if (res.data.count == that.tableData.length) {
            console.log("已经是最后一页了");
            this.$message({
              message: "已经是最后一页了",
              type: "warning",
              duration: "2000",
            });
          }
        } else if (res.code == 400) {
          this.$message({
            message: res.message,
            type: "warning",
            duration: "2000",
          });
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => {});
    },
    // 将搜索条件格式处理
    formToSearch(formData) {
      // school_name: "", // 学校名称
      //   respondent: "", // 被访者姓名
      //   visitor_name: "", //访客姓名
      //   plate_number: "",
      //   time: "",
      this.searchData.school_name = formData.school_name;
      this.searchData.respondent = formData.respondent;
      this.searchData.visitor_name = formData.visitor_name;
      this.searchData.plate_number = formData.plate_number;
      // this.searchData.department_id = formData.department_id;
      // this.searchData.token = this.$VueCookies.get("token");
      this.searchData.page = 1;
      this.searchData.pagesize = 20;
      if (formData.time == "" || formData.time == null || formData.time == []) {
        this.searchData.end_time = "";
        this.searchData.begin_time = "";
      } else {
        this.searchData.end_time = formData.time[1];
        this.searchData.begin_time = formData.time[0];
      }
      return this.searchData;
    },
    submitForm() {
      let that = this;
      let loadingInstance = Loading.service({
        target: document.querySelector(".el-table__body-wrapper"),
      });
      console.log(this.formToSearch(this.formData));
      console.log(this.formData);
      visitorLog(this.formToSearch(this.formData)).then((res) => {
        console.log(res);
        if (res.code == 200) {
          console.log(res.data.res);
          res.data.res.forEach((item, index) => {
            res.data.res[index].create_time = that
              .$moment(item.create_time * 1000)
              .format("YYYY-MM-DD HH:mm:ss");
            this.tableData = res.data.res;
            // console.log(moment(item.create_time * 1000).format("YYYY-MM-DD"));
          });
          that.total = res.data.count;
          this.currentPage = 1; // 重置到第一页
          loadingInstance.close();
          console.log(that.tableData.length);
        } else if (res.code == 400) {
          this.$message({
            message: res.message,
            type: "warning",
            duration: "2000",
          });
        }
      });
    },
    resetForm() {
      this.$refs["elForm"].resetFields();
      // this.formToSearch(this.formData);
      this.searchData = {
        school_name: "", // 学校名称
        respondent: "", // 被访者姓名
        visitor_name: "", //访客姓名
        plate_number: "",
        page: "1",
        pagesize: "20",
      };
      this.initVisitorLog();
    },
    handel(e) {
      console.log("11");
      console.log(e);
      this.Visible = e;
      this.visitorVisible = e;
    },
    Confirm(e) {
      console.log("11");
      console.log(e);
      this.Visible = e;
      this.visitorVisible = e;
    },
    handleClick(row) {
      console.log(row);
      this.Visible = !this.Visible;
      this.visitorVisible = !this.visitorVisible;
      this.detaile = row;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.searchData.pagesize = val;
      this.searchData.page = 1;
      this.currentPage = 1;
      this.initVisitorLog();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.searchData.page = val;
      this.initVisitorLog();
      this.currentPage = val;
    },
  },

  created() {},
};
</script>
<style scoped lang="scss">
#elform {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
  /deep/.el-form-item__content {
    margin-left: 30px !important;
  }
}
.warn-top {
  // border: 1px red solid;
  padding: 10px;
  background-color: white;
  border-radius: 10px;
  width: 100%;
  height: auto;
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  align-content: center;
  justify-content: space-around;
  align-items: flex-start;
  box-sizing: border-box;
}
.warn-top {
  width: calc(100%);
}
// .auto-width {
//   width: "auto";
// }
.el-table {
  width: calc(100%);
}
.warn-content {
  margin-top: 20px;
  // border: 1px red solid;
  height: auto;
  background-color: white;
  border-radius: 10px;
  padding: 10px;
  box-sizing: border-box;
}
// /deep/ .has-gutter .thead {
//   border: 1px red solid !important;
// }
// /deep/.el-table .thead {
//   border: 1px red solid !important;
// }
.el-table .el-table__cell {
  padding: 12px 0;
  min-width: 0;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: center;
}
/deep/.el-table__header-wrapper {
  // border: 1px red solid !important;
  border-radius: 10px 10px 0 0;
}
/deep/.el-table {
  border: 1px #d7dbe3 solid;
  border-radius: 10px;
}
/deep/.has-gutter tr th {
  color: black;
  text-align: center;
  border: 1px d7dbe3 solid;
  background-color: #ecf5ff;
}
/deep/.is-leaf .el-table__cell {
  border: 1px red solid !important;
}
/deep/.is-leaf {
  border: 1px #d7dbe3 solid !important;
  border-top: none !important;
  border-left: none !important;
}
/deep/.el-table__cell {
  text-align: center !important;
}

/deep/ .el-dialog {
  border-radius: 15px !important;
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  margin-top: 0;
}
/deep/.el-dialog__header {
  text-align: left;
  font-weight: 600;
}
/deep/.el-dialog__body {
  padding: 20px !important;
}
/deep/.el-input__inner {
  border: 1px solid #d7dbe3 !important;
  background-color: white !important;
}
/deep/.el-range-editor.el-input__inner {
  position: relative !important;
  right: 47px !important;
}
/deep/.el-input--medium .el-input__inner {
  position: relative !important;
}
.upIpfo {
  display: flex;
  width: 100%;
  //
  justify-content: space-between;
}
.upIpfo p {
  color: #13213b;
  font-size: 14px;
  font-weight: 400;
  width: 50%;
  margin-top: 10px;
  //
  display: inline-block;
  // background-color: skyblue;
}
</style>
